<?php

namespace nimo\common\csv;

class SimpleConverter implements CsvConverter {
    protected $rules = null;
    public function __construct(array $rules = array()) {
        if (count($rules) > 0) {
            $this->rules = $rules;
        }
    }
    public function convert($data) {
        if ($this->rules == null) {
            // return all when rule is empty
            return $data;
        }
        $a = array();
        foreach ( $this->rules as $key => $convert ) {
            $value = null;
            if (is_numeric($convert)) {
                $value = $data[$convert];
            } elseif (is_callable($convert)) {
                $value = $convert($data);
            }
            $a[$key] = $value;
        }
        return $a;
    }
}
